package org.bedracket.pgeg.init;

import net.minecraft.network.chat.Component;
import net.minecraft.world.item.*;
import net.minecraft.world.item.enchantment.Enchantment;
import net.minecraft.world.item.enchantment.EnchantmentCategory;
import net.minecraft.world.item.enchantment.EnchantmentInstance;
import net.minecraftforge.event.CreativeModeTabEvent;
import org.bedracket.pgeg.item.FoodItem;
import org.bedracket.pgeg.item.FoodSeedItem;
import org.bedracket.pgeg.util.Helpers;

import java.util.EnumSet;
import java.util.Set;

public class ModCreativeModeTabs {

    public static CreativeModeTab TOOL;
    public static CreativeModeTab MATERIAL;
    public static CreativeModeTab FOOD;
    public static CreativeModeTab NATURAL_BLOCK;

    public static void onTabCreate(CreativeModeTabEvent.Register event) {
        Set<Item> items = Helpers.getItems();
        TOOL = event.registerCreativeModeTab(Helpers.identifier("tool"),
                builder -> builder.icon(() -> new ItemStack(ModItems.SATURNIUM_PICKAXE.get()))
                        .displayItems(((features, output, hasPermissions) -> {
                            Set<EnchantmentCategory> enchantmentCategories = EnumSet.allOf(EnchantmentCategory.class);
                            generateEnchantmentBookTypesAllLevels(output, enchantmentCategories, CreativeModeTab.TabVisibility.PARENT_TAB_ONLY);
                            generateEnchantmentBookTypesOnlyMaxLevel(output, enchantmentCategories, CreativeModeTab.TabVisibility.SEARCH_TAB_ONLY);
                            items.stream().filter(item -> item instanceof TieredItem)
                                    .forEach(output::accept);
                            items.stream().filter(item -> item instanceof ArmorItem)
                                    .forEach(output::accept);
                            output.accept(ModItems.BAGUA_COMPASS.get());
                            output.accept(ModItems.TALISMAN.get());
                        })).title(Component.translatable("itemGroup.pgeg.tool")).build());
        MATERIAL = event.registerCreativeModeTab(Helpers.identifier("material"),
                builder -> builder.icon(() -> new ItemStack(ModItems.AIR_SHARD.get()))
                        .displayItems(((features, output, hasPermissions) -> {
                            output.accept(ModItems.MANA_CRYSTAL_DUST.get());
                            items.stream().filter(item -> item.getDescriptionId().contains("ingot"))
                                    .forEach(output::accept);
                            items.stream().filter(item -> item.getDescriptionId().contains("shard"))
                                    .forEach(output::accept);
                            items.stream().filter(item -> item.getDescriptionId().contains("_diagram"))
                                    .forEach(output::accept);
                            items.stream().filter(item -> item.getDescriptionId().contains("_yao"))
                                    .forEach(output::accept);
                        })).title(Component.translatable("itemGroup.pgeg.material")).build());
        FOOD = event.registerCreativeModeTab(Helpers.identifier("food"),
                builder -> builder.icon(() -> new ItemStack(ModItems.COOKIE_CAT.get()))
                        .displayItems(((features, output, hasPermissions) -> {
                            items.stream().filter(item -> item instanceof FoodItem)
                                    .forEach(output::accept);
                            items.stream().filter(item -> item instanceof FoodSeedItem)
                                    .forEach(output::accept);
                        })).title(Component.translatable("itemGroup.pgeg.food")).build());
        NATURAL_BLOCK = event.registerCreativeModeTab(Helpers.identifier("natural_block"),
                builder -> builder.icon(() -> new ItemStack(ModBlocks.FIRE_INFUSED_STONE.get()))
                        .displayItems(((features, output, hasPermissions) -> {
                            output.accept(ModBlocks.AIR_INFUSED_STONE.get());
                            output.accept(ModBlocks.AQUA_INFUSED_STONE.get());
                            output.accept(ModBlocks.EARTH_INFUSED_STONE.get());
                            output.accept(ModBlocks.ENTROPY_INFUSED_STONE.get());
                            output.accept(ModBlocks.FIRE_INFUSED_STONE.get());
                            output.accept(ModBlocks.ORDER_INFUSED_STONE.get());
                            output.accept(ModBlocks.AIR_INFUSED_DEEPSLATE.get());
                            output.accept(ModBlocks.AQUA_INFUSED_DEEPSLATE.get());
                            output.accept(ModBlocks.EARTH_INFUSED_DEEPSLATE.get());
                            output.accept(ModBlocks.ENTROPY_INFUSED_DEEPSLATE.get());
                            output.accept(ModBlocks.FIRE_INFUSED_DEEPSLATE.get());
                            output.accept(ModBlocks.ORDER_INFUSED_DEEPSLATE.get());
                            output.accept(ModBlocks.BRIMSTONE_ORE.get());
                            output.accept(ModBlocks.METEORITE_ORE.get());
                            output.accept(ModBlocks.MITHRIL_ORE.get());
                            output.accept(ModBlocks.ORICHALCUM_ORE.get());
                            output.accept(ModBlocks.SATURNIUM_ORE.get());
                            output.accept(ModBlocks.SILVER_ORE.get());
                            output.accept(ModBlocks.DEEPSLATE_BRIMSTONE_ORE.get());
                            output.accept(ModBlocks.DEEPSLATE_METEORITE_ORE.get());
                            output.accept(ModBlocks.DEEPSLATE_MITHRIL_ORE.get());
                            output.accept(ModBlocks.DEEPSLATE_ORICHALCUM_ORE.get());
                            output.accept(ModBlocks.DEEPSLATE_SATURNIUM_ORE.get());
                            output.accept(ModBlocks.DEEPSLATE_SILVER_ORE.get());
                        })).title(Component.translatable("itemGroup.pgeg.natural_block")).build());
    }

    private static void generateEnchantmentBookTypesAllLevels(CreativeModeTab.Output pOutput, Set<EnchantmentCategory> pEnchantmentCategories, CreativeModeTab.TabVisibility pTabVisibility) {
        Set<Enchantment> enchantments = Helpers.getEnchantments();
        enchantments.stream().filter(enchantment -> enchantment.allowedInCreativeTab(Items.ENCHANTED_BOOK, pEnchantmentCategories))
                .forEach(enchantment -> {
                    for (int i = enchantment.getMinLevel(); i <= enchantment.getMaxLevel(); ++i) {
                        pOutput.accept(EnchantedBookItem.createForEnchantment(new EnchantmentInstance(enchantment, i)), pTabVisibility);
                    }
                });
    }

    private static void generateEnchantmentBookTypesOnlyMaxLevel(CreativeModeTab.Output pOutput, Set<EnchantmentCategory> pEnchantmentCategories, CreativeModeTab.TabVisibility pTabVisibility) {
        Set<Enchantment> enchantments = Helpers.getEnchantments();
        enchantments.stream().filter(enchantment -> enchantment.allowedInCreativeTab(Items.ENCHANTED_BOOK, pEnchantmentCategories))
                .forEach(enchantment -> {
                    pOutput.accept(EnchantedBookItem.createForEnchantment(new EnchantmentInstance(enchantment, enchantment.getMaxLevel())), pTabVisibility);
                });
    }
}
